<template>
	<view id="lab-detail">
		<cu-custom bgColor="bg-informatic-brown" isBack>
			<block slot="backText">返回</block>
			<block slot="content">讲座报名</block>
		</cu-custom>
		<view class="cu-list menu-avatar">
			<view class="cu-item" v-for="lecture in lectures" :key="lecture.ID" @click="toDetail(lecture.ID)">
				<view class="cu-avatar round lg" :style="`background-image:url(${app.webInfo.avatar});`"></view>
				<view class="content">
					<view>
						<view class="text-black text-cut">{{lecture.Name}}</view>
					</view>
					<view class="text-gray text-sm flex">
						<view class="text-cut">
							{{lecture.SignUpBegin}} - {{lecture.SignUpEnd}}<br />地点：{{lecture.Address}}
						</view>
					</view>
				</view>
				<view class="action">
					<view class="cu-tag round bg-grey sm">{{lecture.state}}</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	const app = require("@/config");
	export default{
		data() {
			return {
				lectures: [],
				app
			}
		},
		onLoad() {
			this.getLectures();
		},
		methods: {
			getLectures() {
				uni.post("/api/activity/GetUserActivities", {}, msg => {
					if(msg.success) {
						this.lectures = msg.data;
						for (var i = 0; i < this.lectures.length; i++) {
							this.calcState(this.lectures[i]);
						}
					} else {
						uni.showToast({
							title: msg.msg,
							icon: "none"
						})
					}
				});
			},
			toDetail(id){
				uni.navigateTo({
					url: `./detail?id=${id}`
				});
			},
			calcState(data) {
				var time = new Date();
				if (time < new Date(data.SignUpBegin)) {
					data.state = "未开始";
				} else if (time < new Date(data.SignUpEnd)) {
					data.state = "报名中";
				} else if (time > new Date(data.SignUpEnd)) {
					data.state = "报名结束";
				}
			},
			dateFormat(fmt, date) {
				if(date instanceof Date){
					
				}else{
					date = new Date(date);
				}
				
			    let ret;
			    const opt = {
			        "Y+": date.getFullYear().toString(),        // 年
			        "m+": (date.getMonth() + 1).toString(),     // 月
			        "d+": date.getDate().toString(),            // 日
			        "H+": date.getHours().toString(),           // 时
			        "M+": date.getMinutes().toString(),         // 分
			        "S+": date.getSeconds().toString()          // 秒
			        // 有其他格式化字符需求可以继续添加，必须转化成字符串
			    };
			    for (let k in opt) {
			        ret = new RegExp("(" + k + ")").exec(fmt);
			        if (ret) {
			            fmt = fmt.replace(ret[1], (ret[1].length == 1) ? (opt[k]) : (opt[k].padStart(ret[1].length, "0")))
			        };
			    };
			    return fmt;
			}
		}
	}
</script>

<style>
</style>
